'use strict'

class content {

  constructor () {
  }
  async list(c) {
    let sqltext = `SELECT * FROM reply LEFT JOIN users on reply.open_id=users.open_id order by reply_time desc`;
    let r = await c.service.psql.query(sqltext);
    c.send(JSON.stringify(r.rows));
  }
  async post(c) {
    let u=c.box.user;
    let reply_time=Date.now();
    // let reply_id=c.helper.makeId();
    let sqltext = `INSERT INTO reply(open_id,reply_text,reply_time,reply_id) VALUES($1,$2,$3,$4)`;
    let r = await c.service.psql.query(sqltext,[u.openid,c.body.text,reply_time,c.body.reply_id]);
    c.send(JSON.stringify(r.rows));
  }

  async get(c) {
    let sqltext = `SELECT * FROM reply WHERE reply_id = $1 order by reply_time desc`;
    let r = await c.service.psql.query(sqltext, [c.param.id]);
    if (r.rowCount <= 0) return c.status(404).send('not found');
    c.send(JSON.stringify(r.rows[0]));
  }

}

module.exports = content;
